package com.dlnu.shoping.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dlnu.shoping.shopping.common.Result;
import com.dlnu.shoping.shopping.entity.ShoppingCart;
import com.dlnu.shoping.shopping.entity.TestUser;
import com.dlnu.shoping.shopping.mapper.ShoppingCartDao;
import com.dlnu.shoping.shopping.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartDao shoppingCartDao;


    @Override
    public Result searchByUseId(Integer useId) {
        return new Result(shoppingCartDao.search(useId));
    }

    @Override
    public Result delete(Integer useId, Integer proId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("use_id",useId);
        wrapper.eq("pro_id",proId);
        shoppingCartDao.delete(wrapper);
        return Result.SUCCESS;
    }

    @Override
    public Result addCart(Integer useId, Integer proId,Integer proNum,String size) {
        ShoppingCart cart = new ShoppingCart();
        cart.setUseId(useId);
        cart.setProId(proId);
        cart.setPro_num(proNum);
        if(StringUtils.isNotEmpty(size)) {
            cart.setClothesSize(size);
        }
        cart.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shoppingCartDao.insert(cart);
        return Result.SUCCESS;
    }

    @Override
    public Result alterProNum(Integer useId, Integer proId, Integer proNum) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("use_id",useId);
        wrapper.eq("pro_id",proId);
        ShoppingCart cart = shoppingCartDao.selectOne(wrapper);
        cart.setPro_num(proNum);
        shoppingCartDao.updateById(cart);
        return Result.SUCCESS;
    }
}
